package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hngm.bean.Productpicture;
import com.hngm.common.Result;
import com.hngm.common.ResultList;
import com.hngm.dao.ProductDao;
import com.hngm.dao.ProductpictureDao;
import com.hngm.util.StringUtil;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class PictureServlet
 */
public class PictureServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PictureServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//获取请求中的参数url
		String url = request.getParameter("url");
		ProductpictureDao productpictureDao = new ProductpictureDao();
		if("picList".equals(url)){
			//获取前端传过来的产品id
			String productId = request.getParameter("productId");
			
			List<Productpicture> picList = productpictureDao.queryPicListByPid(productId);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", 0, picList);
			out.print(str);
		}else if("add".equals(url)){
			//进行图片上传
			//使用smartupload组件实现文件上传
			//1、创建一个SmartUpload对象                                       
			SmartUpload smart = new SmartUpload();
			//2、设置字符编码
			smart.setCharset("utf-8");
			//3、初始化
			smart.initialize(getServletConfig(),request,response);
			
			try {
				//4、文件上传
				smart.upload();
				//5、文件保存到服务器端本地
				String name = System.currentTimeMillis() + ".jpg";	
				smart.getFiles().getFile(0).saveAs("/productpic/" + name);
				//图片上传成功之后，将图片信息和产品id插入到数据库中
				//获取前端传过来的产品Id
				String productId = request.getParameter("productId");
				if(StringUtil.isNotNullorEmpty(productId)){//判断产品ID不为空，在进行插入操作
					Productpicture pic = new Productpicture();
					pic.setPicurl("productpic/" + name);//图片路径
					pic.setProduct_id(Long.parseLong(productId));//产品id
					int result = productpictureDao.insertPropicture(pic);
					
					if(result > 0){
						//图片上传成功之后，要把图片的名称返回给前端页面
						String json = Result.toClient(true, "图片上传成功！",name);
						out.print(json);
					}else{
						//图片上传成功之后，要把图片的名称返回给前端页面
						String json = Result.toClient(true, "图片上传失败！",name);
						out.print(json);
					}
				}
				
				//图片上传成功之后，要把图片的名称返回给前端页面
//				String json = Result.toClient(true, "图片上传成功！",name);
//				out.print(json);
			} catch (SmartUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				String json = Result.toClient(false, "图片上传失败！","图片格式不正确");
				out.print(json);
			}	
		}else if("del".equals(url)){
			//删除产品逻辑
			//1、获取前端传过来的产品Id
			String productId = request.getParameter("productId");
			//2、根据产品Id删除产品信息
			int result = productpictureDao.delProductpiture(productId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		}
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
